#include "./../inc/handle.h"

#include <string.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

int g_handle[UFO_ID_END][UFO_MAX_HANDLE];
void* g_handleObj[UFO_ID_END][UFO_MAX_HANDLE];
int   g_handleCount[UFO_ID_END];

UFO_Handle ufoCreateHandle(int id, void* object)
{
	UFO_Handle handle = 0;
	if (id >= UFO_ID_END)
		return handle;

	g_handleCount[id]++;
	
	handle = (UFO_Handle)&g_handle[id][g_handleCount[id]];
	handle->unused = id<<16;
	handle->unused |= g_handleCount[id];

	g_handleObj[id][g_handleCount[id]] = object;
	return handle;
}

void* ufoObjectFromHandle(UFO_Handle handle)
{
    int id = handle->unused>>16;
    int index = handle->unused&0x0000FFFF;
	return g_handleObj[id][index];
}

int ufoCloseHandle(UFO_Handle handle)
{
	//free(handle);
	return 0;
}